package org.midy.infrastructure.beas;

import java.nio.ByteBuffer;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BeansUtils {
    private static final Map<Class<? extends Object>, Map<String, Object>> BEAN_CONTAINER = new ConcurrentHashMap<>();

    public static <T> T getBean(Class<? extends Object> clazz, String name) {
        Map<String, Object> stringObjectMap = BEAN_CONTAINER.get(clazz);
        if (stringObjectMap != null) {
            return (T) stringObjectMap.get(name);
        }
        return null;
    }

    public static void beanRegister(Class<? extends Object> clazz, String name, Object bean) {
        Map<String, Object> stringObjectMap = BEAN_CONTAINER.get(clazz);
        if (stringObjectMap == null) {
            stringObjectMap = new ConcurrentHashMap<>();
            stringObjectMap = BEAN_CONTAINER.putIfAbsent(clazz, stringObjectMap);
        }
        stringObjectMap.putIfAbsent(name, bean);
    }
}
